﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Utility
{
    public class myConverter
    {
        static string GetString(byte[] b)
        {
            return System.Text.Encoding.Default.GetString(b);
        }
        static string GetHexString(byte[] b)
        {
            StringBuilder sb = new StringBuilder();
            char[] baseline = "0123456789ABCDEF".ToArray();

            //char i = 0; /*for numStr---------*/
            byte temp;
            for (int i = 0; i < b.Length; i++)
            {
                temp = b[i];
                do
                {
                    sb.Append(baseline[temp % 16]);
                    temp /= 16;
                    if (temp == 0 && sb.Length == 1)
                        sb.Append(baseline[0]);
                } while (0 != temp);
            }

            return sb.ToString();
        }

        public static string GetHexString(byte[] b, int index, int length)
        {
            if (index > b.Length | index + length > b.Length)
            {
                return "";
            }
            StringBuilder sb = new StringBuilder();
            char[] baseline = "0123456789ABCDEF".ToArray();

            //char i = 0; /*for numStr---------*/
            byte temp;

            for (int i = index; i < length; i++)
            {
                temp = b[i];
                sb.Append(baseline[(temp >> 4 & 0x0f)]);
                sb.Append(baseline[(temp & 0x0f)]);
                sb.Append(" ");
            }

            return sb.ToString();
        }
        public static string GetHexString(byte[] b, int index, int length, char spilt)
        {
            if (index > b.Length | index + length > b.Length)
            {
                return "";
            }
            StringBuilder sb = new StringBuilder();
            char[] baseline = "0123456789ABCDEF".ToArray();

            //char i = 0; /*for numStr---------*/
            byte temp;

            for (int i = index; i < length; i++)
            {
                temp = b[i];
                sb.Append(baseline[(temp >> 4 & 0x0f)]);
                sb.Append(baseline[(temp & 0x0f)]);
                sb.Append(spilt);
            }

            return sb.ToString();
        }


        private static byte GetBaseHex(char d)
        {
            if (d >= '0' && d <= '9')
            {
                return (byte)(d - '0');
            }
            else if (d >= 'a' && d <= 'f')
            {
                return (byte)(d - 'a' + 10);
            }
            else if (d >= 'A' && d <= 'F')
            {
                return (byte)(d - 'A' + 10);
            }
            else
                return 0;
        }
        public static byte[] GetByteByInputHex(string _data)
        {
            List<byte> list = new List<byte>();
            char[] baseline = "0123456789ABCDEF".ToArray();
            char[] data = _data.ToArray();
            byte temp = 0;
            for (int i = 0; i < data.Length;)
            {
                if (data[i] == ' ')
                {
                    i++;
                    continue;
                }
                if (i + 1 >= data.Length)
                {
                    temp = GetBaseHex(data[i]);
                    i += 1;
                }
                else
                {
                    if (data[i + 1] == ' ')
                    {
                        temp = GetBaseHex(data[i]);
                        i += 1;
                    }
                    else
                    {
                        temp = (byte)(GetBaseHex(data[i]) << 4 | GetBaseHex(data[i + 1]));
                        i += 2;
                    }
                }
                list.Add(temp);
            }
            return list.ToArray();
        }
    }
}
